package listbyorder.access101_200.test138;

import java.util.HashMap;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/11 11:29
 */
public class Solution2 {

    public Node copyRandomList(Node head) {
        if (head == null) return head;

        HashMap<Node, Node> map = new HashMap<>();

        Node temp = head;

        // 首先把节点都赛到map中去
        while (temp != null) {
            Node t = new Node(temp.val);
            map.put(temp, t);
            temp = temp.next;
        }

        temp = head;
        while (temp != null) {
            if (temp.next != null) {
                map.get(temp).next = map.get(temp.next);
            }
            if (temp.random != null) {
                map.get(temp).random = map.get(temp.random);
            }
            temp = temp.next;
        }

        return map.get(head);
    }
}
